package com.dl.aspect;

import org.apache.shiro.authz.AuthorizationException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.servlet.ModelAndView;
import org.thymeleaf.exceptions.TemplateInputException;

import javax.servlet.http.HttpServletRequest;

/**
 * 全局异常处理类
 * @author DL
 * @create 2021-05-26 15:58
 */
@ControllerAdvice
public class GlobalExceptionHandler {

    /**
     * 全局处理shiro角色异常信息
     * @param req
     * @param e
     * @return
     * @throws Exception
     */
    @ExceptionHandler(value = AuthorizationException.class) //@ExceptionHandler 该注解声明异常处理方法
    public ModelAndView shiroRoleException(HttpServletRequest req, Exception e) throws Exception {
        ModelAndView mv = new ModelAndView();
        mv.addObject("exception", e);  //异常内容(页面展示)
        mv.addObject("url", req.getRequestURL()); //请求的url地址(页面展示)
        mv.addObject("msg","shiro角色异常，没有权限操作");
        mv.setViewName("error/unauthorized"); //设置视图名称
        return mv;
    }

/*    @ExceptionHandler(value = TemplateInputException.class) //@ExceptionHandler 该注解声明异常处理方法
    public ModelAndView templateInputException(HttpServletRequest req, Exception e) throws Exception {
        ModelAndView mv = new ModelAndView();
        mv.addObject("exception", e);  //异常内容(页面展示)
        mv.addObject("url", req.getRequestURL()); //请求的url地址(页面展示)
        mv.addObject("msg","shiro角色异常，没有权限操作");
        mv.setViewName("error/unauthorized"); //设置视图名称
        return mv;
    }*/
}
